package p.lodz.pl.poid.zad2.controller;

import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import p.lodz.pl.poid.zad2.fourier.FourierProcessor;

/**
 * FXML Controller class
 *
 * @author janiu
 */
public class FFTPanelController implements Initializable {
    
    @FXML
    private ToggleGroup typeGroup;
    
    @FXML
    private CheckBox fastFourierTransform;
    
    @FXML
    private CheckBox fastFourierTransformInverse;
    
    @FXML
    private RadioButton fftFrequency;
    
    @FXML
    private RadioButton fftPhase;
    
    @FXML
    private TextField decimation;
    
    @FXML
    private Button perform;
    
    private ImageView resultImage;
    private Image image;
    private BufferedImage bufferedImage;

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
    }    

    @FXML
    private void performFastFourierTransform(ActionEvent event) {
        FourierProcessor processor = new FourierProcessor();
        FFTData data = prepareData();
        BufferedImage bufferedImage = processor.fourierTransform(this.bufferedImage, data);
        Image image = Image.impl_fromExternalImage(bufferedImage);
        resultImage.setImage(image);
    }
    
    private FFTData prepareData() {
        FFTData data = new FFTData();
        boolean isFFTMarked = fastFourierTransform.isSelected();
        boolean isIFFTMarked = fastFourierTransformInverse.isSelected();
        boolean isPhaseMarked = fftPhase.isSelected();
        boolean isFrequencyMarhed = fftFrequency.isSelected();
        boolean isDecimationEmpty = this.decimation.getText().isEmpty();
        String decimationText = this.decimation.getText();
        decimationText = isDecimationEmpty ? "0" : decimationText;
        Integer decimation = Integer.parseInt(decimationText);
        data.setFftMarked(isFFTMarked);
        data.setIfftMarked(isIFFTMarked);
        data.setPhaseMarked(isPhaseMarked);
        data.setFrequencyMarked(isFrequencyMarhed);
        data.setDecimationEmpty(isDecimationEmpty);
        data.setDecimation(decimation);
        return data;
    }
    
    public void setResultImage(ImageView resultImage) {
        this.resultImage = resultImage;
    }

    public void setImage(Image image) {
        this.image = image;
    }

    public void setBufferedImage(BufferedImage bufferedImage) {
        this.bufferedImage = bufferedImage;
    }

}
